<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

function check_array($array)
{
	return (is_array($array) && count($array) > 0) ? true : false;
}

function client_ip()
{
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
	{
		$onlineip = getenv('HTTP_CLIENT_IP');
	}
	else if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
	{
		$onlineip = getenv('HTTP_X_FORWARDED_FOR');
	}
	else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
	{
		$onlineip = getenv('REMOTE_ADDR');
	}
	else if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
	{
		$onlineip = $_SERVER['REMOTE_ADDR'];
	}

	preg_match('/[\d\.]{7,15}/', $onlineip, $onlineipmatches);
	$onlineip = ($onlineipmatches[0] ? $onlineipmatches[0] : 'unknown');

	return $onlineip;
}

function referer($default = '?')
{
	$DOMAIN = preg_replace("~^www\.~", '', strtolower(getenv('HTTP_HOST') ? getenv('HTTP_HOST') : $_SERVER['HTTP_HOST']));
	
	$referer = isset($_POST['referer']) ? $_POST['referer'] : (isset($_GET['referer']) ? $_GET['referer'] : '');
	
	if($referer == '')$referer = $_SERVER['HTTP_REFERER'];
	
	return $referer;
}

function random($length, $numeric = 0)
{
	mt_srand((double)microtime() * 1000000);
	if($numeric)
	{
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	}
	else
	{
		$hash = '';
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++)
		{
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}

function cut_str($string, $length, $dot = ' ...')
{
	if(strlen($string) <= $length)
	{
		return $string;
	}

	$strcut = '';
	if(CHARSET == 'utf-8')
	{
		$n = $tn = $noc = 0;
		while($n < strlen($string))
		{
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126))
			{
				$tn = 1; $n++; $noc++;
			}
			else if(194 <= $t && $t <= 223)
			{
				$tn = 2; $n += 2; $noc += 2;
			}
			else if(224 <= $t && $t < 239)
			{
				$tn = 3; $n += 3; $noc += 2;
			}
			else if(240 <= $t && $t <= 247)
			{
				$tn = 4; $n += 4; $noc += 2;
			}
			else if(248 <= $t && $t <= 251)
			{
				$tn = 5; $n += 5; $noc += 2;
			}
			else if($t == 252 || $t == 253)
			{
				$tn = 6; $n += 6; $noc += 2;
			}
			else
			{
				$n++;
			}

			if($noc >= $length)
			{
				break;
			}
		}

		if($noc > $length)
		{
			$n -= $tn;
		}

		$strcut = substr($string, 0, $n);
	}
	else
	{
		for($i = 0; $i < $length; $i++)
		{
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}

	return $strcut.$dot;
}


function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
{
	$ckey_length = 4;
	if(!$key)
    {
		$key = SEC_AUTHKEY;
	}
	
	$key = md5($key);
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	$rndkey = array();
	for($i = 0; $i <= 255; $i++)
	{
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	for($j = $i = 0; $i < 256; $i++)
	{
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++)
	{
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE')
	{
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16))
		{
			return substr($result, 26);
		}
		else
		{
			return '';
		}
	}
	else
	{
		return $keyc.str_replace('=', '', base64_encode($result));
	}
}

function image_path($id)
{
	$key = "o"."y"."e"."k"."e"."y"."_"."n"."i"."k"."m";
	$hash = md5($key."\t".$id."\t".strlen($id)."\t".$id % 10);
	$path = $hash{$id % 32} . "/" . abs(crc32($hash) % 100) . "/";

	return $path;
}

function size_convert($filesize)
{
	if ($filesize >= 1073741824)
	{
		$filesize = round($filesize / 1073741824 , 2) . "G";
	}
	else if($filesize >= 1048576)
	{
		$filesize = round($filesize / 1048576, 2) . "M";
	}
	else if($filesize >= 1024)
	{
		$filesize = round($filesize / 1024, 2) . "K";
	}
	else
	{
		$filesize = $filesize . "B";
	}
	
	return $filesize;
}

function js_alert_output($alert_msg, $halt=true)
{
    echo "<script language='javascript'>alert('{$alert_msg}');</script>";
    if($halt)exit;
}

function post_request($url, $request)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	$result = curl_exec($ch);
	curl_close($ch);
	
	return $result;
}